package com.fourierlab.ibator.plugins;

import java.util.ArrayList;
import java.util.List;

import org.apache.ibatis.ibator.api.GeneratedJavaFile;
import org.apache.ibatis.ibator.api.IbatorPluginAdapter;
import org.apache.ibatis.ibator.api.IntrospectedTable;
import org.apache.ibatis.ibator.api.dom.java.FullyQualifiedJavaType;
import org.apache.ibatis.ibator.api.dom.java.Interface;
import org.apache.ibatis.ibator.api.dom.java.JavaVisibility;
import org.apache.ibatis.ibator.api.dom.java.TopLevelClass;
import org.apache.ibatis.ibator.internal.util.StringUtility;
import org.apache.ibatis.ibator.internal.util.messages.Messages;

public class DaoPlugin extends IbatorPluginAdapter {
	public static String DEFAULT_DAO_PATTERN = "I%sDao";
	public static String DEFAULT_DAO_IMPL_PATTERN = "%sDaoImpl";
	public static String DAO_PATTERN = "daoPattern";
	public static String DAO_IMPL_PATTERN = "daoImplPattern";

	public DaoPlugin() {
		super();
	}

	/* (non-Javadoc)
	 * @see org.apache.ibatis.ibator.api.IbatorPlugin#validate(java.util.List)
	 */
	public boolean validate(List<String> warnings) {
        boolean valid = true;
        
        if (!StringUtility.stringHasValue(properties.getProperty("targetProject"))) {
            warnings.add(Messages.getString("Warning.25"));
            valid = false;
        }
        
        if (!StringUtility.stringHasValue(properties.getProperty("targetExtPackage"))) {
            warnings.add(Messages.getString("Warning.26"));
            valid = false;
        }
        
        if (!StringUtility.stringHasValue(properties.getProperty("targetPackage"))) {
            warnings.add(Messages.getString("Warning.26"));
            valid = false;
        }
        
        return valid;
	}

	/* (non-Javadoc)
	 * @see org.apache.ibatis.ibator.api.IbatorPluginAdapter#contextGenerateAdditionalJavaFiles(org.apache.ibatis.ibator.api.IntrospectedTable)
	 */
	@Override
	public List<GeneratedJavaFile> contextGenerateAdditionalJavaFiles(
			IntrospectedTable introspectedTable) {
		Interface interfaze = new Interface(calculateDao(introspectedTable));
		interfaze.addImportedType(introspectedTable.getDAOInterfaceType());
		interfaze.addSuperInterface(introspectedTable.getDAOInterfaceType());
		interfaze.setVisibility(JavaVisibility.PUBLIC);
		
		TopLevelClass topLevelClass = new TopLevelClass(calculateDaoImpl(introspectedTable));
		topLevelClass.addImportedType(introspectedTable.getDAOImplementationType());
		topLevelClass.setSuperClass(introspectedTable.getDAOImplementationType());
		topLevelClass.addSuperInterface(interfaze.getType());
		topLevelClass.setVisibility(JavaVisibility.PUBLIC);
		
		List<GeneratedJavaFile> answer = new ArrayList<GeneratedJavaFile>(2);
		answer.add(new GeneratedJavaFile(interfaze, properties.getProperty("targetProject")));
		answer.add(new GeneratedJavaFile(topLevelClass, properties.getProperty("targetProject")));
		
		return answer;
	}
	
	private FullyQualifiedJavaType calculateDaoImpl(IntrospectedTable introspectedTable) {
		return new FullyQualifiedJavaType(properties.getProperty("targetExtPackage") + "." + String.format(properties.getProperty(DAO_IMPL_PATTERN, DEFAULT_DAO_IMPL_PATTERN), introspectedTable.getFullyQualifiedTable().getDomainObjectName()));
	}
	
	private FullyQualifiedJavaType calculateDao(IntrospectedTable introspectedTable) {
		return new FullyQualifiedJavaType(properties.getProperty("targetExtPackage") + "." + String.format(properties.getProperty(DAO_PATTERN, DEFAULT_DAO_PATTERN), introspectedTable.getFullyQualifiedTable().getDomainObjectName()));
	}
}
